<template>
  <nav class="navbar navbar-dark bg-primary justify-content-between mb-4 px-4">
    <router-link class="navbar-brand" to="/">知乎专栏</router-link>
    
    <!-- 没登录 就显示登录注册按钮 -->
    <ul v-if="!user.isLogin" class="list-inline mb-0">
      <li class="list-inline-item">
        <router-link to="/login" class="btn btn-outline-light my-2">登录</router-link>
      </li>
      <li class="list-inline-item">
        <router-link to="/login" class="btn btn-outline-light my-2">注册</router-link>
      </li>
    </ul>

    <!-- 已登录  显示登录的用户名 -->
    <ul v-else class="list-inline mb-0">
      <li class="list-inline-item">
        <dropdown :title="`你好 ${user.name}`">
          <dropdown-item>
            <router-link to="/create" class="dropdown-item">新建文章</router-link>
          </dropdown-item>

          <dropdown-item disabled
            ><a href="#" class="dropdown-item">编辑文章</a></dropdown-item
          >
          
          <dropdown-item
            ><a href="#" class="dropdown-item">退出登录</a></dropdown-item
          >
          <!-- 实现了结构化语义 充分的扩展，未来可以添加自定义事件，扩展出更多的功能 -->
        </dropdown>
      </li>
    </ul>
  </nav>
</template>

<script lang="ts">
import { defineComponent, PropType } from "vue";
import Dropdown from "./Dropdown.vue";
import DropdownItem from "./DropdownItem.vue";
export interface UserProps {
  // 用户登录
  isLogin: boolean;
  // 用户名
  name?: string;
  // 唯一标识 id
  id?: number;
}

export default defineComponent({
  name: "GlobalHeader",
  components: {
    Dropdown,
    DropdownItem,
  },
  
  props: {
    //接受 父组件传递过来的数据
    user: {
      // 构造函数断言成类型 使用ProType
      type: Object as PropType<UserProps>,

      // required：Boolean，该 prop 是否是必填项
      required: true,
    },
  },
});
</script>
